package com.guoliang.wechatpushing.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoliang.wechatpushing.constant.Constant;
import com.guoliang.wechatpushing.service.WeatherService;
import com.guoliang.wechatpushing.utils.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Leo
 * @date ：Created in 2022/10/18 10:38
 * @description：
 */
@Service
public class WeatherServiceImpl implements WeatherService {
    @Autowired
    Constant constant;
    @Override
    public JSONObject nowWeather() {
        //#请求示例https://www.yiketianqi.com/free/day?appid=97327392&appsecret=p5VVCPKH&unescape=1
        String url="https://www.yiketianqi.com/free/day";
        Map<String,Object> params=new HashMap<>();
        params.put("appid",constant.wId);
        params.put("appsecret", constant.wAppsecret);
        params.put("city", constant.city);
        JSONObject jsonObject = RestUtil.sendGetRequest(RestUtil.getRestTemplate(), url, params, JSONObject.class);
//        System.out.println(jsonObject);
        return jsonObject;
    }

    @Override
    public String[] nextWeather() {
        String[] weathers=new String[3];
        String url="https://www.yiketianqi.com/free/week";
        Map<String,Object> params=new HashMap<>();
        params.put("appid",constant.wId);
        params.put("appsecret", constant.wAppsecret);
        params.put("city", constant.city);
        params.put("unescape",1);
        JSONObject jsonObject = RestUtil.sendGetRequest(RestUtil.getRestTemplate(), url, params, JSONObject.class);
//        System.out.println(jsonObject);
        JSONArray weatherArray = (JSONArray)((JSONArray) jsonObject.get("data"));
        weathers[0] = ((JSONObject) weatherArray.get(0)).getString("wea");
        weathers[1] = ((JSONObject) weatherArray.get(1)).getString("wea");
        weathers[2] = ((JSONObject) weatherArray.get(2)).getString("wea");
        return weathers;
    }
    /*
    * {"date":"2022-10-18","tem_day":"23","win_meter":"17km/h",
    * "week":"星期二","city":"珠海","tem_night":"16","cityid":"101280701",
    * "air":"66","pressure":"1007","update_time":"14:31","wea":"小雨",
    * "wea_img":"yu","win_speed":"3级","humidity":"90%","tem":"17","win":"东北风","nums":4}
     */
}
